package com.mdd.common.util.weifengqi.encryptionPassword;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.codec.Base64;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;

//@Log4j
@Slf4j
public class AESUtils {

    public static final String KEY_ALGORITHM = "AES";
    public static final String ENCODING = "utf-8";
    public static final String KEY = "SYZXjz5otdNNyig/uKXt+w==";

    public static String generateAESKey() {
        KeyGenerator keyGenerator = null;
        try {
            keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        keyGenerator.init(128);
        SecretKey key = keyGenerator.generateKey();
        byte[] keyExternal = key.getEncoded();
        return Base64.encodeBase64String(keyExternal);
    }

    public static String encrypt(String content, String key) {
        try {
            byte[] bytesKey = Base64.decodeBase64(key.getBytes());
            SecretKeySpec secretKey = new SecretKeySpec(bytesKey, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);// 创建密码器
            byte[] byteContent = content.getBytes(ENCODING);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);// 初始化
            byte[] result = cipher.doFinal(byteContent);// 加密
            return Base64.encodeBase64String(result);
        } catch (Exception e) {
            log.error("encrypt error", e);
        }
        return null;
    }

    public static String decrypt(String content, String key) {
        try {
            byte[] bytesKey = Base64.decodeBase64(key.getBytes());
            SecretKeySpec secretKey = new SecretKeySpec(bytesKey, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, secretKey);// 初始化
            byte[] result = cipher.doFinal(Base64.decodeBase64(content.getBytes()));// 解密
            return new String(result);
        } catch (Exception e) {
            log.error("decrypt error", e);
        }
        return null;
    }

    /**
     * 微风企：
     * 对已经封装好的参数进行加密
     *
     * @param message
     * @return
     */
    public static String encryptForSys(JSONObject message) throws UnsupportedEncodingException {
        String content = message.toJSONString();
        String key = generateAESKey();
        key = KEY;
        log.info("key：" + key);
        log.info("加密前：" + content);
        // 加密
        String encryptResult = encrypt(content, key);
        log.info("加密后：" + encryptResult);

        String encryptResultBase64 = new String(Base64.encodeBase64(encryptResult.getBytes()), ENCODING);
        log.info("base64后：" + encryptResultBase64);

        return encryptResultBase64;
    }


    /**
     * 微风企：
     * 参数解密
     *
     * @param message
     * @return
     */
    public static String decryptForSys(String message) throws Exception {
//        log.info("URL：http://xxxxxx?message={}", URLEncoder.encode(message));
        String key = generateAESKey();
        key = KEY;
        // 解密
        String decryptResult = decrypt(new String(Base64.decodeBase64(message.getBytes()), ENCODING), key);
        if (decryptResult != null) {
            log.info("解密后：" + new String(decryptResult));
        }
        return decryptResult;
    }



    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        JSONObject message = new JSONObject();
        message.put("orderNo", "FSD203108011140605800001890001");
        message.put("companyName", "杭州微风企科技有限公司");

        String content = message.toJSONString();
        String key = generateAESKey();
        key = KEY;
        log.info("key：" + key);
        log.info("加密前：" + content);
        // 加密
        String encryptResult = encrypt(content, key);
        log.info("加密后：" + encryptResult);

        String encryptResultBase64 = new String(Base64.encodeBase64(encryptResult.getBytes()), ENCODING);
        log.info("base64后：" + encryptResultBase64);

        log.info("URL：http://xxxxxx?message={}", URLEncoder.encode(encryptResultBase64));

        // 解密
        String decryptResult = decrypt(new String(Base64.decodeBase64(encryptResultBase64.getBytes()), ENCODING), key);
        log.info("解密后：" + new String(decryptResult));
    }



}

